{extend name='table'}

{block name="button"}

<button data-modal='{:url("add")}' data-title="{:lang('添加用户')}" class='layui-btn layui-btn-sm layui-btn-primary'>{:lang('添加用户')}</button>

{/block}

{block name="content"}
<div class="layui-tab layui-tab-card">

    <div class="layui-tab-content">
        {include file='base/menu/power_search'}
        <table id="IndexPower" data-url="{:sysuri('power')}" data-target-search="form.form-search"></table>
    </div>
</div>
<script>
    $(function () {
        $('#IndexPower').layTable({
            even: true, height: 'full',
            sort: {field: 'id', type: 'desc'},
            where: {menu_id: '{$menu_id|default=0}'},
            cols: [[
                {field: 'sort', title: '{:lang("排序")}', width: 100, align: 'center', sort: true, templet: '#SortInputTpl'},
                {field: 'node', title: '{:lang("节点")}', minWidth: 100, align: 'center'},
                {field: 'title', title: '{:lang("名字")}', minWidth: 100, align: 'center'},
                {field: 'is_selected', title: '{:lang("使用状态")}', align: 'center', minWidth: 110, templet: '#CheckedSwitchTpl'},
                {toolbar: '#toolbarPower', title: '{:lang("操作面板")}', minWidth: 150, align: 'center', fixed: 'right'},
            ]]
        });


        var util = layui.util;
        //  2.9+ 版本可省略 attr 参数，默认读取 lay-on
        util.on({
            add: function(){

                let node  = $('input[name=url]').val();
                let title = $('input[name=title]').val();
                let menu_id = "{$menu_id}";

                $.ajax({
                    type:'post',
                    url: "{:sysuri('nodesAdd')}",
                    data: {node:node, title:title, menu_id:menu_id},
                    success:function (res) {

                        if (res.code == 1) {
                            $.msg.success(res.info, 3, function () {
                                layui.table.reloadData('IndexPower', {});
                            });
                        }
                    }
                })

                // layer.msg('触发了事件 1');
            },
            e2: function(){
                // layer.msg('触发了事件 2');
            }
        });

        // 数据状态切换操作
        layui.form.on('switch(StatusSwitch)', function (obj) {

            var data = {id: obj.value, is_selected: obj.elem.checked > 0 ? 1 : 0};
            $.form.load("{:url('nodesStatus')}", data, 'post', function (ret) {
                if (ret.code < 1) $.msg.error(ret.info, 3, function () {
                    $('#IndexPower').trigger('reload');
                }); else {
                    $('#IndexPower').trigger('reload')
                }
                return false;
            }, false);
        });
    });
</script>

<!-- 数据状态切换模板 -->
<script type="text/html" id="CheckedSwitchTpl">


    <input type="checkbox" value="{{d.id}}"  lay-skin="switch" lay-text="{:lang('已激活')}|{:lang('已禁用')}" lay-filter="StatusSwitch" {{-d.is_selected > 0 ? 'checked': ''}} >

</script>

<!-- 列表排序权重模板 -->
<script type="text/html" id="SortInputTpl">
    <input type="number" min="0" data-blur-number="0" data-action-blur="{:sysuri()}" data-value="id#{{d.id}};action#sort;sort#{value}" data-loading="false" value="{{d.sort}}" class="layui-input text-center">
</script>

<!-- 操控面板的模板 -->
<script type="text/html" id="toolbarPower">

    <a class="layui-btn layui-btn-sm layui-btn-danger" data-confirm="确定要删除菜单吗？" data-action="{:url('nodesRemove')}" data-value="id#{{d.id}}">删 除</a>

</script>

{/block}
